<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\File;
use think\Session;
class User extends Controller
{
    public function welcome()
    {   
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        if(empty($user_id)){

            $this->error("您还没登录，请先登录","login/login");
        }else{

            return $this->fetch();
        }	
    }
    public function userinfo(){

        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $data = Db("user")->where("user_id",$user_id)->find();
        //  echo "<pre>";
        // print_r($data);die;
        $this->assign('data',$data);
    	return $this->fetch();
    }

    public function updateto(){

       $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $data = input("post.");
        //  echo "<pre>";
        // print_r($data);die;
        $res = Db("user")->where('user_id',$user_id)->update($data);
        if($res){

            $this->success('成功');
        }else{

            $this->error('失败');
        }
    }

    public function updatetoto(){

       $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $new_password = input("post.new_password");
        $res = Db("user")->where('user_id',$user_id)->update(['user_password'=>$new_password]);
        if($res){

            $this->success('成功');
        }else{

            $this->error('失败');
        }
    }

    public function dingdan()
    {
        header("content-type:text/html;charset=UTF-8");
       $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $data = Db("order")->where('user_id',$user_id)->order('addtime desc')->select();
         
        $this->assign("data",$data);
        // $this->assign("user_id",$user_id);
    	return $this->fetch();
    }

    public function dingdanlist(){

        header("content-type:text/html;charset=UTF-8");
        //$order_id = input("get.order_id");
        //$order_id = 1;
        $order_id = input("get.order_id");
       // echo "<pre>";
        $res['data'] = Db("order")->where("order_id", $order_id)->select();
        $res['goods']=Db("goods_order")->where("order_id", $order_id)->select();
       // var_dump($res['data']);die;
        return $this->fetch('dingdanlist',['data'=>$res]);
    }

    public function shouhuodelete(){
        $id = input('get.id');
        $res=Db::query("delete  from recipient where id = '$id' ");
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $data = Db("recipient")->where("user_id", $user_id)->order('id desc')->find();
        if(empty($data)){
            return $this->fetch('shouhuo');die;
        }
        $country=$data['country'];
        $province=$data['province'];
        $city=$data['city'];
        $district=$data['district'];    
        $country=Db::query("select region_name  from region where region_id= '$country'");
        $data['country']=$country[0]['region_name'];
        $province=Db::query("select region_name  from region where region_id= '$province'");
        $data['province']=$province[0]['region_name'];
        $city=Db::query("select region_name from  region where region_id= '$city'");
        $data['city']=$city[0]['region_name'];
        $district=Db::query("select region_name  from region where region_id= '$district'");
        $data['district']=$district[0]['region_name'];
        $this->assign('data',$data);

        return $this->fetch('shouhuo');
        
    }

    public function shouhuo()
    {
         header("content-type:text/html;charset=UTF-8");
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $data = Db("recipient")->where("user_id", $user_id)->order('id desc')->find();
        if(empty($data)){
            return $this->fetch();die;
        }
        $country=$data['country'];
        $province=$data['province'];
        $city=$data['city'];
        $district=$data['district'];    
        $country=Db::query("select region_name  from region where region_id= '$country'");
        $data['country']=$country[0]['region_name'];
        $province=Db::query("select region_name  from region where region_id= '$province'");
        $data['province']=$province[0]['region_name'];
        $city=Db::query("select region_name from  region where region_id= '$city'");
        $data['city']=$city[0]['region_name'];
        $district=Db::query("select region_name  from region where region_id= '$district'");
        $data['district']=$district[0]['region_name'];
        $this->assign('data',$data);

        return $this->fetch();
    }

    public function address(){

        header("content-type:text/html;charset=UTF-8");
        $parent_id = input("post.parent_id");
        $data = Db("region")->where("parent_id",$parent_id)->select();
        echo json_encode($data);
    }


    public function addshouhuo(){

        header("content-type:text/html;charset=UTF-8");
        $data = input('post.');
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        if(empty($data['country'][0]) || empty($data['country'][1]) ||empty($data['country'][2]) ||empty($data['country'][3])  ){
            echo "<script>alert('请认真填写地址');history.back()</script>";die;
        }
        $info=[
            'country'=>$data['country'][0],
            'province'=>$data['country'][1],
            'city'=>$data['country'][2],
            'district'=>$data['country'][3],
            'consignee'=>$data['consignee'],
            'email'=>$data['email'],
            'address'=>$data['address'],
            'zipcode'=>$data['zipcode'],
            'tel'=>$data['tel'],
            'mobile'=>$data['mobile'],
            'user_id'=>$user_id
        ];
      
        $res = Db("recipient")->insert($info);
        if($res){

            $this->success('成功');
        }else{

            $this->error('失败');
        }
        // var_dump($data);die;
    }

    public function shouhuoupdate(){
        header("content-type:text/html; charset=utf-8");
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $data = input('post.');
        //var_dump($data);die;
        //   echo "<pre>";
        // print_r($data);die;
        $res = Db("recipient")->where('user_id',$user_id)->update($data);
        if($res){

            $this->success('成功');
        }else{

            $this->error('失败');
        }
    }
    
    


    public function shoucang()
    {
        return $this->fetch();
    }
    public function liuyan()
    {   
         header("content-type:text/html;charset=UTF-8");
        $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $data = Db("msg")->where("user_id",$user_id)->select();
        //    echo "<pre>";
        // print_r($data);die;
        $this->assign("data",$data);
        return $this->fetch();
    }




    public function liuyanto()
    {   

      $user_info = session::get("user_info");
        $user_id = $user_info['user_id'];
        $data=input('post.');
        $file=request()->file('msg_img');
        $info=$file->move(ROOT_PATH.'public'.DS.'uploads');
        // var_dump($info);
        $filename=$info->getSaveName();
        $data['msg_img']=$filename;
        $data['user_id'] = $user_id;
        $data['time'] = date("Y-m-d H:i:s",time());
        // var_dump($data);
        $res=Db('msg')->insert($data);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }

    public function biaoqian(){
        return $this->fetch();
    }
}